import { MockMethod } from 'vite-plugin-mock'
import dayjs from 'dayjs'
import { cloneDeep } from 'lodash-es'
import { builder, getRequestToken, requestParams } from '../../_util'

type Member = {
  avatar: string;
  name: string;
  id: string;
};

type BasicListItemDataType = {
  id: string;
  owner: string;
  title: string;
  avatar: string;
  cover: string;
  status: 'normal' | 'exception' | 'active' | 'success';
  percent: number;
  logo: string;
  href: string;
  body?: any;
  updatedAt: string;
  createdAt: string;
  subDescription: string;
  description: string;
  activeUser: number;
  newUser: number;
  star: number;
  like: number;
  message: number;
  content: string;
  members: Member[];
};

const titles = [
  'Alipay',
  'Angular',
  'Ant Design',
  'Ant Design Pro',
  'Bootstrap',
  'React',
  'Vue',
  'Webpack'
]
const avatars = [
  'https://gw.alipayobjects.com/zos/rmsportal/WdGqmHpayyMjiEhcKoVE.png', // Alipay
  'https://gw.alipayobjects.com/zos/rmsportal/zOsKZmFRdUtvpqCImOVY.png', // Angular
  'https://gw.alipayobjects.com/zos/rmsportal/dURIMkkrRFpPgTuzkwnB.png', // Ant Design
  'https://gw.alipayobjects.com/zos/rmsportal/sfjbOqnsXXJgNCjCzDBL.png', // Ant Design Pro
  'https://gw.alipayobjects.com/zos/rmsportal/siCrBXXhmvTQGWPNLBow.png', // Bootstrap
  'https://gw.alipayobjects.com/zos/rmsportal/kZzEzemZyKLKFsojXItE.png', // React
  'https://gw.alipayobjects.com/zos/rmsportal/ComBAopevLwENQdKWiIn.png', // Vue
  'https://gw.alipayobjects.com/zos/rmsportal/nxkuOJlFJuAUhzlMTCEe.png' // Webpack
]

const covers = [
  'https://gw.alipayobjects.com/zos/rmsportal/uMfMFlvUuceEyPpotzlq.png',
  'https://gw.alipayobjects.com/zos/rmsportal/iZBVOIhGJiAnhplqjvZW.png',
  'https://gw.alipayobjects.com/zos/rmsportal/iXjVmWVHbCJAyqvDxdtx.png',
  'https://gw.alipayobjects.com/zos/rmsportal/gLaIAoVWTtLbBWZNYEMg.png'
]
const desc = [
  '那是一种内在的东西， 他们到达不了，也无法触及的',
  '希望是一个好东西，也许是最好的，好东西是不会消亡的',
  '生命就像一盒巧克力，结果往往出人意料',
  '城镇中有那么多的酒馆，她却偏偏走进了我的酒馆',
  '那时候我只会想自己想要什么，从不想自己拥有什么'
]

const user = [
  '付小小',
  '曲丽丽',
  '林东东',
  '周星星',
  '吴加好',
  '朱偏右',
  '鱼酱',
  '乐哥',
  '谭小仪',
  '仲尼'
]

function basicList(count: number): BasicListItemDataType[] {
  const list: BasicListItemDataType[] = []
  for (let i = 0; i < count; i += 1) {
    list.push({
      id: `basic-list-${i}`,
      owner: user[i % 10],
      title: titles[i % 8],
      avatar: avatars[i % 8],
      cover: parseInt(`${i / 4}`, 10) % 2 === 0 ? covers[i % 4] : covers[3 - (i % 4)],
      status: [ 'active', 'exception', 'normal' ][i % 3] as
        | 'normal'
        | 'exception'
        | 'active'
        | 'success',
      percent: Math.ceil(Math.random() * 50) + 50,
      logo: avatars[i % 8],
      href: 'https://ant.design',
      updatedAt: dayjs(new Date(new Date().getTime() - 1000 * 60 * 60 * 2 * i).getTime())
        .format('YYYY-MM-DD HH:mm:ss'),
      createdAt: dayjs(new Date(new Date().getTime() - 1000 * 60 * 60 * 2 * i).getTime())
        .format('YYYY-MM-DD HH:mm:ss'),
      subDescription: desc[i % 5],
      description:
        '在中台产品的研发过程中，会出现不同的设计规范和实现方式，但其中往往存在很多类似的页面和组件，这些类似的组件会被抽离成一套标准规范。',
      activeUser: Math.ceil(Math.random() * 100000) + 100000,
      newUser: Math.ceil(Math.random() * 1000) + 1000,
      star: Math.ceil(Math.random() * 100) + 100,
      like: Math.ceil(Math.random() * 100) + 100,
      message: Math.ceil(Math.random() * 10) + 10,
      content:
        '段落示意：蚂蚁金服设计平台 ant.design，用最小的工作量，无缝接入蚂蚁金服生态，提供跨越设计与开发的体验解决方案。蚂蚁金服设计平台 ant.design，用最小的工作量，无缝接入蚂蚁金服生态，提供跨越设计与开发的体验解决方案。',
      members: [
        {
          avatar: 'https://gw.alipayobjects.com/zos/rmsportal/ZiESqWwCXBRQoaPONSJe.png',
          name: '曲丽丽',
          id: 'member1'
        },
        {
          avatar: 'https://gw.alipayobjects.com/zos/rmsportal/tBOxZPlITHqwlGjsJWaF.png',
          name: '王昭君',
          id: 'member2'
        },
        {
          avatar: 'https://gw.alipayobjects.com/zos/rmsportal/sBxjgqiuHMGRkIjqlQCd.png',
          name: '董娜娜',
          id: 'member3'
        }
      ]
    })
  }

  return list
}

let sourceData: BasicListItemDataType[] = basicList(100)

function getBasicList(params) {

  const { pageNum = 1, pageSize = 5 } = params

  let result = cloneDeep(sourceData)

  if (params.title) {
    result = result.filter((data) => data.title.includes(params.title || ''))
  }

  if (params.status) {
    if (params.status !== 'all')
      result = result.filter((data) => data.status === params.status)
  }

  let finalPageSize = 5
  if (params.pageSize) {
    finalPageSize = parseInt(`${params.pageSize}`, 10)
  }

  result = result.slice(
    ((pageNum as number) - 1) * (pageSize as number),
    (pageNum as number) * (pageSize as number)
  )
  return {
    list: result,
    total: sourceData.length,
    pageSize: finalPageSize,
    pageNum: parseInt(`${params.pageNum}`, 10) || 1
  }
}

function postBasicList(body, type) {
  const { id } = body

  switch (type) {
    case 'delete':
      sourceData = sourceData.filter((item) => item.id !== id)
      break
    case 'update':
      sourceData = sourceData.map((item) => {
        if (item.id === id) {
          return { ...item, ...body }
        }
        return item
      })
      break
    case 'add':
      sourceData.unshift({
        ...body,
        id: `basic-list-${sourceData.length}`,
        createdAt: dayjs(new Date().getTime()).format('YYYY-MM-DD HH:mm:ss')
      })
      break
    default:
      break
  }
}

export default [
  {
    url: '/mock-server/get_basic_list',
    timeout: 200,
    method: 'post',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      return builder(token, {
        data: cloneDeep(getBasicList(request.body))
      })
    }
  },
  {
    url: '/mock-server/basic_list_info',
    timeout: 200,
    method: 'post',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      const { id } = request.body
      return builder(token, {
        data: sourceData.find(item => item.id === id),
        code: sourceData.some(item => item.id === id) ? 200 : 500,
        msg: '请传入正确的key值！'
      })
    }
  },
  {
    url: '/mock-server/post_basic_list',
    timeout: 200,
    method: 'post',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      postBasicList(request.body, 'add')
      return builder(token)
    }
  },
  {
    url: '/mock-server/post_basic_list',
    timeout: 200,
    method: 'put',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      postBasicList(request.body, 'update')
      return builder(token)
    }
  },
  {
    url: '/mock-server/post_basic_list',
    timeout: 200,
    method: 'delete',
    response: (request: requestParams) => {
      const token = getRequestToken(request)
      postBasicList(request.body, 'delete')
      return builder(token)
    }
  }
] as MockMethod[]
